#!/bin/bash

# vector -Startup script for the Vector router
#
# description: A lightweight and ultra-fast tool for building observability pipelines
# processname: vector
# config:      /etc/vector/vector.toml
# pidfile:     /var/run/vector.pid

### BEGIN INIT INFO
# Provides:          vector
# Required-Start:    $network $local_fs
# Required-Stop:     $network $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: A lightweight and ultra-fast tool for building observability pipelines
# Description:       A lightweight and ultra-fast tool for building observability pipelines
### END INIT INFO

# This script is based off of https://bash.cyberciti.biz/guide//etc/init.d

# Source function library.
. /etc/rc.d/init.d/functions

# This will prevent initlog from swallowing up a pass-phrase prompt if
# mod_ssl needs a pass-phrase from the user.
INITLOG_ARGS=""

# Path to the apachectl script, server binary, and short-form for messages.
vector=/usr/bin/vector
prog=vector
pidfile=${PIDFILE-/var/run/vector.pid}
RETVAL=0

# Start
start() {
  echo -n $"Starting $prog: "
  daemon --pidfile=${pidfile} $vector $OPTIONS
  RETVAL=$?
  echo
  return $RETVAL
}

# Stop with a 10 second wait period
stop() {
  echo -n $"Stopping $prog: "
  # Handles removing the pidfile
  killproc -p ${pidfile} -d 10 $vector
  RETVAL=$?
  echo
  return $RETVAL
}

# Reload
reload() {
  echo -n $"Reloading $prog: "
  killproc -p ${pidfile} $vector -HUP
  RETVAL=$?
  echo
  return $RETVAL
}

# See how we were called.
case "$1" in
  start)
    start
  ;;
  stop)
    stop
  ;;
  status)
    status -p ${pidfile} $vector
    RETVAL=$?
  ;;
  restart)
    stop
    start
  ;;
  condrestart)
    if [ -f ${pidfile} ] ; then
      stop
      start
    fi
  ;;
  reload)
    reload
  ;;
  help)
    $vector $@
    RETVAL=$?
  ;;
  *)
    echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|help}"
    exit 1
esac

exit $RETVAL
